import java.util.ArrayList;
import java.util.List;

public class Text6 {
    // 438.找到字符串中的所有字母异位词
    public List<Integer> findAnagrams(String s, String p) {
        List<Integer> ret = new ArrayList<>();

        char[] ss = s.toCharArray();
        char[] pp = p.toCharArray();
        int[] hash_p = new int[26];
        int[] hash_s = new int[26];

        for(char a : pp) hash_p[a - 'a']++;

        int count = 0, n = pp.length;
        for (int left = 0,right = 0;right < ss.length; right++){
            char in = ss[right];
            if (++hash_s[in - 'a'] <= hash_p[in - 'a'])
                count++;
            //hash_s[in - 'a']++;

            if (right - left + 1 > n){
                char out = ss[left];
                left++;
                if (hash_s[out - 'a']-- <= hash_p[out - 'a'])
                    count--;
            }

            if (n == count)
                ret.add(left);
        }
        return ret;
    }
}
